package com.cos.consumer.controller;

import com.cos.common.entity.Student;
import com.cos.consumer.feign.SupplierFeignClient;
import com.cos.consumer.services.SupplierServices;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: COS
 * @time: 2022/4/27 14:16
 * @description:
 */
@RestController
@RequestMapping("/con")
public class ConsumerController {
    //面向接口编程
    @Resource
    private SupplierFeignClient supplierFeignClient;
    @Autowired
    private SupplierServices supplierServices;


    @GetMapping("/getVersion")
    public String getVersion() throws InterruptedException {
//        System.out.println(10/0);
//        Thread.sleep(5000);
        return "version:v2";
    }
    @GetMapping("/getRouter")
    public String getRouter() throws InterruptedException {
//        System.out.println(10/0);
//        Thread.sleep(5000);
        return "跳转了";
    }

    @GetMapping("/test")
    public Student findDate2(){
        //调用一个业务方法
        Student date2 = supplierServices.findDate2(1);
        System.out.println(supplierServices.findDate2(1));
        System.out.println(supplierServices.findDate2(1));
        supplierServices.clearFindById(1);
        System.out.println(supplierServices.findDate2(1));
        System.out.println(supplierServices.findDate2(1));
        return null;
    }


    @GetMapping("/findData")
    public String findDate(@PathVariable("id") Integer id){
        System.out.println(10/0);
        System.out.println(Thread.currentThread().getName());
        String data = supplierFeignClient.getData();
        return "找到你了===>"+data;
    }


    @GetMapping("/getById/{id}")
//    @HystrixCommand(fallbackMethod = "getStudentByIdFallBack",
//    commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "5000"),
//            @HystrixProperty(name = "execution.timeout.enabled",value = "true")
//    })//如果出现异常情况 超时 报错执行对应的服务降级方法
//    @HystrixCommand(fallbackMethod = "getStudentByIdFallBack",
//    commandProperties = {
//            @HystrixProperty(name = "execution.isolation.strategy",value = "SEMAPHORE"),//设置信号量隔离
//            @HystrixProperty(name = "execution.isolation.semaphore.maxConcurrentRequests",value = "10")
//    })//如果出现异常情况 超时 报错执行对应的服务降级方法
    @HystrixCommand(fallbackMethod = "getStudentByIdFallBack",
    commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "70"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000")
    })//如果出现异常情况 超时 报错执行对应的服务降级方法
    public Student getStudentById(@PathVariable("id") Integer id) throws InterruptedException {
        if (id==1) {
            System.out.println(10 / 0);
        }
//        Thread.sleep(3000);
        System.out.println(Thread.currentThread().getName());
        Student studentById = supplierFeignClient.getStudentById(id);
        System.out.println(studentById);
        return studentById;
    }

    public Student getStudentByIdFallBack(Integer id){
        System.out.println("出现异常情况 ");
        return new Student(-1,"无名氏", "不明");
    }
    //?
    @GetMapping("/getStudent")
    public Student getStudent(@RequestParam(value = "sno") Integer id,
                              @RequestParam("name")String name,
                              @RequestParam String sex){
        return supplierFeignClient.getStudent(id, name, sex);

    }
    @GetMapping("/addStudent")
    public Student addStudent(Student student){
        return supplierFeignClient.addStudent(student);
    }
    @GetMapping("/getStuList")
    public List<Student> addStudent() {
        return supplierFeignClient.getStuList();
    }
}
